


/*

Table B14 and others

*/


local nametable = "tables/rev_cropchoice"


forvalues xd=1(1)1{
	
	if `xd'==1{
		local xdist = 50
	}
	if `xd'==2{
		local xdist = 25
	}
	if `xd'==3{
		local xdist = 100
	}


	
	forvalues xlin = 1(1)1{
	
	*-----
	eststo clear
	local column_number = 1
	local column_name = "column" + "`column_number'"
	*---	
	

	if `xlin'==0{
		local poly = "No"
		local xcontrols 	= "  "
	}
	if `xlin'==1{	
		local poly = "Linear"
		local xcontrols		= " rdist1*   "
	}
	
	use "public_data/g30.dta", clear
	
	drop if adist>`xdist'

	local varlistc = "cassavayam_areat sorghum_areat groundnut_areat millet_areat pulses_areat wspotato_areat maize_areat rice_areat wheat_areat cerealoth_areat bancocon_areat cocoacoffeetea_areat sugarcane_areat oilpalm_area"

	foreach varc of varlist `varlistc'{
			
		
		local crop = subinstr("`varc'","_areat","",.)
		local crop = subinstr("`crop'","yam","",.)
		local crop = subinstr("`crop'","wsp","p",.)
		local crop = subinstr("`crop'","bancocon","Banana \& coconut",.)
		local crop = subinstr("`crop'","cocoacoffeetea","Cocoa, coffee \& tea",.)
		local crop = subinstr("`crop'","oilpalm","Oilpalm \& other",.)
		local crop = subinstr("`crop'","cerealoth","Other cereal",.)
		local crop = upper(substr("`crop'",1,1)) + substr("`crop'",2,.)

		
		replace `varc' = `varc'>0 if `varc'<.
		
		label var `varc' "`crop'"
				
		replace weight = 1

		egen interm = mean(`varc')
		local vmeandep = interm[1]
		local meandep = string(`vmeandep')
		if index("`meandep'",".")==1{
			local meandep = "0"+substr("`meandep'",1,3)
		}
		if index("`meandep'",".")<5&index("`meandep'",".")>1{
			local meandep = substr("`meandep'",1,4)
		}
	
*		xi: areg `varc' `xcontrols' varofi  [aw=weight],  a(groupfe) cluster(border)
		reghdfe `varc' `xcontrols'  varofi [aw=weight], absorb(groupfe) vce(cluster groupfe country)
		eststo `column_name'
		estadd local space  				" "
		estadd local maxdist 					"`xdist'"
		estadd local polynomial				"`poly'"
		estadd local nmeandep			"`meandep'"
		estadd local groupfe				"Yes"
		local column_number = `column_number' + 1
		local column_name = "column" + "`column_number'"
		
		drop inter*
	}



	

*label var interdep  				" "		

local nametable1 = "`nametable'" + "fr" + "`xd'" + ".tex"

local depvar= "\textit{Dependent Variable: Crop is grown}"

#delimit ;
esttab column1 column2 column3 column4 column5 column6 column7
using "`nametable1'", replace
label
b(3)
se(3)
star(* 0.10 ** 0.05 *** 0.01)
width(1.0\hsize)
nonotes
nolines
noconstant
mgroups("`depvar'", pattern(1 0 0 0 0 0 0)
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
keep(varofi)
order(varofi)
  stats(groupfe					space	polynomial		space	r2		N					space	maxdist							space	nmeandep,
    fmt(0						0		0				0		3		0					0		0								0		0)
layout(@						@		@				@		@		@					@		@								@		@)	 	
labels("Border x Ethnic FE"		" "		"RD Polynomial"	" "		"R2"	"\# Observations"	" "		"Max distance to border (km)"	" " 	"Mean Dep. Var.")
 )	
noobs
booktabs
;
#delimit cr




local nametable1 = "`nametable'" + "sr" + "`xd'" + ".tex"

local depvar= " "

#delimit ;
esttab column8 column9 column10 column11 column12 column13 column14
using "`nametable1'", replace
label
b(3)
se(3)
star(* 0.10 ** 0.05 *** 0.01)
width(1.0\hsize)
nonotes
nolines
noconstant
mgroups("`depvar'", pattern(1 0 0 0 0 0 0)
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
keep(varofi)
order(varofi)
  stats(groupfe					space	polynomial		space	r2		N					space	maxdist							space	nmeandep,
    fmt(0						0		0				0		3		0					0		0								0		0)
layout(@						@		@				@		@		@					@		@								@		@)	 	
labels("Border x Ethnic FE"		" "		"RD Polynomial"	" "		"R2"	"\# Observations"	" "		"Max distance to border (km)"	" " 	"Mean Dep. Var.")
 )	
noobs
booktabs
;
#delimit cr

	}
}
